"""
====================================================================
Copyright (C) 2012-2013 Eric Blond

This file is part of Neptune.

Neptune is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.

Neptune is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with Neptune.  If not, see <http://www.gnu.org/licenses/>.
====================================================================

Created on 22 Jun 2012

@author: Eric
"""

import wx
from ..nucleus import public as nucleus
from . import utils

#######

class Frame(nucleus.Page):

    def _on_Title_change(self, value):
        self._WxFrame.SetTitle(unicode(value))

    @nucleus.cell(settable = True, on_change = '_on_Title_change')
    def Title(self):
        return ''

    def _on_IsVisible_change(self, value):
        self._WxFrame.Show(bool(value))

    @nucleus.cell(settable = True, on_change = '_on_IsVisible_change')
    def IsVisible(self):
        return True

    @nucleus.cell(settable = True)
    def OnCloseEvent(self):
        return None

    @nucleus.cell(initable = True)
    def MenuBar(self):
        return None

    def _wx_on_close_event(self, event):
        utils.call_user_cb((~self).OnCloseEvent, event)
        if not event.GetVeto():
            self._Parent.Frames = [x for x in self._Parent.Frames if x != self]

    _Parent  = None
    _WxFrame = None
    
    def _bind(self, parent):
        self._Parent = parent
        self._WxFrame = wx.Frame(None, title = self.Title)
        self._WxFrame.Bind(wx.EVT_CLOSE, self._wx_on_close_event)
        if self.IsVisible:
            self._on_IsVisible_change(True)
        if self.MenuBar:
            self.MenuBar._bind(self)
            self._WxFrame.SetMenuBar(self.MenuBar._WxMenuBar)
    
    def _unbind(self):
        if self.MenuBar:
            self.MenuBar._unbind()
        self._WxFrame.Destroy()
        self._WxFrame = None
        self._Parent = None
